package com.bjsxt.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.commons.exception.DaoException;
import com.bjsxt.commons.pojo.BaizhanResult;
import com.bjsxt.mapper.TbItemMapper;
import com.bjsxt.pojo.TbItem;
import com.bjsxt.pojo.TbItemDesc;
import com.bjsxt.pojo.TbItemParamItem;
import com.bjsxt.service.ItemCatService;
import com.bjsxt.service.ItemDescService;
import com.bjsxt.service.ItemParamItemService;
import com.bjsxt.service.ItemService;
import com.bjsxt.utils.IDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author coder_z
 * @date 2023/2/8
 **/
@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private TbItemMapper itemMapper;
    @Autowired
    private ItemDescService itemDescService;
    @Autowired
    private ItemParamItemService itemParamItemService;
    @Autowired
    private ItemCatService itemCatService;
    /**
     * 分页查询
     * @param page
     * @param rows
     * @return
     */
    @Override
    public BaizhanResult showPage(Integer page, Integer rows) {
//        构造的分页
        Page<TbItem> pagewhere = new Page<>(page, rows);
//        分页查询获得的结果
        Page<TbItem> pageResult = itemMapper.selectPage(pagewhere, null);
        Map<String, Object> map = new HashMap<>();
        map.put("total",pageResult.getTotal());
        map.put("result",pageResult.getRecords());
        return BaizhanResult.ok(map);
    }

    /**
     * 商品新增
     * @param item
     * @param desc
     * @param itemParamItem
     * @return
     * @throws DaoException
     */
    @Transactional
    @Override
    public BaizhanResult insert(TbItem item, TbItemDesc desc, TbItemParamItem itemParamItem) throws DaoException {
//        商品自增id
        long id = IDUtils.genItemId();
        Date date = new Date();
//       填充item数据
        item.setId(id);
        item.setStatus(1);
        item.setCreated(date);
        item.setUpdated(date);
        int index = itemMapper.insert(item);
        if(index==1){
//           填充desc表数据
            desc.setItemId(id);
            desc.setCreated(date);
            desc.setUpdated(date);
            BaizhanResult insert = itemDescService.insert(desc);
            if(insert.getStatus()==200){
//           填充itemparamitem表数据
                itemParamItem.setItemId(id);
                itemParamItem.setId(IDUtils.genItemId());
                itemParamItem.setCreated(date);
                itemParamItem.setUpdated(date);
                BaizhanResult insert1 = itemParamItemService.insert(itemParamItem);
                if(insert1.getStatus()==200){
                return BaizhanResult.ok();
                }
            }
        }
        return BaizhanResult.error("服务器端消息-新增失败");
    }

    /**
     * 修改商品状态
     * @param item
     * @return
     */
    @Override
    public BaizhanResult updateStatusById(TbItem item) {
        item.setUpdated(new Date());
        int index = itemMapper.updateById(item);
        if(index==1){
            return  BaizhanResult.ok();
        }
        return BaizhanResult.error("服务器端消息-修改商品状态失败");
    }

    /**
     * 根据商品id查询商品信息 商品分类信息  商品描述 商品规格参数信息
     * @param id
     * @return
     */
    @Override
    public BaizhanResult selectAllById(long id) {
        TbItem tbItem = itemMapper.selectById(id);
        BaizhanResult itemCatResult = itemCatService.selectById(tbItem.getCid());
        BaizhanResult itemDescResult = itemDescService.selectById(id);
        BaizhanResult itemParamItemResult = itemParamItemService.selectById(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("itemCat",itemCatResult.getData());
        map.put("item",tbItem);
        map.put("itemDesc",itemDescResult.getData());
        map.put("itemParamItem",itemParamItemResult.getData());
        return BaizhanResult.ok(map);
    }

    @Override
    public BaizhanResult updateById(TbItem item) {
        int index = itemMapper.updateById(item);
        if(index==1){
            return BaizhanResult.ok();
        }
        return BaizhanResult.error("服务器端消息-修改商品信息表失败");
    }
    @Transactional//修改要加事务
    @Override
    public BaizhanResult updateAllById(TbItem item,TbItemDesc desc,TbItemParamItem paramItem)throws DaoException{
//        填充商品信息
        Date date = new Date();
        item.setUpdated(date);
//        直接调用当前类方法 根据Id直接修改赋给对象
        BaizhanResult baizhanResult = updateById(item);
        if(baizhanResult.getStatus()==200){
//        修改商品描信息
            desc.setItemId(item.getId());
            desc.setUpdated(date);
            BaizhanResult descResult = itemDescService.updateById(desc);
            if(descResult.getStatus()==200){
//                修改商品规格参数信息
                paramItem.setItemId(item.getId());
                paramItem.setUpdated(date);
                BaizhanResult paramItemResult = itemParamItemService.updateByItemId(paramItem);
                if(paramItemResult.getStatus()==200){
                    return BaizhanResult.ok();
                }

            }

        }
        throw new DaoException("修改数据失败");
    }
}
